package com.dc.controller;


import com.alibaba.fastjson.JSON;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class BusinessExceptionResolver extends SimpleMappingExceptionResolver {

    @Override
    public ModelAndView resolveException
            (HttpServletRequest request,
             HttpServletResponse response,
             Object handler, Exception ex) {

        Map<String,Object>map=new HashMap<String, Object>();
        map.put("error",ex.getMessage());
        try{
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().append(JSON.toJSONString(map));
        }catch (IOException e){
            e.printStackTrace();
        }

        return super.resolveException(request, response, handler, ex);
    }
}
